package utd.cs6322.cluster.visualization;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ToolTipManager;

import net.sf.javaml.clustering.FarthestFirst;
import net.sf.javaml.clustering.KMeans;
import net.sf.javaml.clustering.KMedoids;
import net.sf.javaml.core.Dataset;

import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.assignment.ShapeAction;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.action.layout.graph.NodeLinkTreeLayout;
import prefuse.action.layout.graph.TreeLayout;
import prefuse.activity.Activity;
import prefuse.controls.ControlAdapter;
import prefuse.controls.DragControl;
import prefuse.controls.NeighborHighlightControl;
import prefuse.controls.PanControl;
import prefuse.controls.WheelZoomControl;
import prefuse.controls.ZoomControl;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Tree;
import prefuse.data.io.DataIOException;
import prefuse.data.io.GraphMLReader;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;
import utd.cs6322.cluster.document.Document;

public class PrefuseTreeVisualizer {
	Graph tree = null;
	Display disp = null;
	Visualization vis = null;

	public PrefuseTreeVisualizer(){
		
		
		TreeBuilder builder = new TreeBuilder();
		Document[] docs = { new Document("A"),
				new Document("B"), new Document( "A"),
				new Document("B"), new Document( "A"),
				new Document("C"), new Document("A"),
				new Document( "C"), new Document( "A"),
				new Document( "C") };
		Document[] cluster1 = { docs[0], docs[2], docs[4], docs[6], docs[8] };
		Document[] cluster2 = { docs[1], docs[3] };
		Document[] cluster3 = { docs[5], docs[7], docs[9] };

		Node root = builder.setRootDocs(docs);
		builder.addCluster(root, cluster1);
		builder.addCluster(root, cluster2);
		Node node1 = builder.addCluster(root, cluster3);
		builder.addCluster(node1, cluster2);
		builder.addCluster(node1, cluster3);
		
		setTree(builder.getTree());
		

		// add the graph to the visualization as the data group "graph"
		// nodes and edges are accessible as "graph.nodes" and "graph.edges"
		vis = new Visualization();
		vis.add("tree", tree);

		// -- 3. the renderers and renderer factory ---------------------------

		// draw the "name" label for NodeItems
		LabelRenderer r = new LabelRenderer("maxclass");
		r.setRoundedCorner(8, 8); // round the corners

		// create a new default renderer factory
		// return our name label renderer as the default for all non-EdgeItems
		// includes straight line edges for EdgeItems by default
		vis.setRendererFactory(new DefaultRendererFactory(r));

		// -- 4. the processing actions ---------------------------------------

		// map nominal data values to colors using our provided palette
		/*
		 * DataColorAction fill = new DataColorAction("tree.nodes", "gender",
		 * Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
		 */
		// ShapeAction shape = new ShapeAction("tree.nodes",
		// Constants.SHAPE_ELLIPSE);
		ColorAction fill = new ColorAction("tree.nodes", VisualItem.FILLCOLOR,
				ColorLib.rgb(51, 204, 255));
		// use black for node text
		ColorAction text = new ColorAction("tree.nodes", VisualItem.TEXTCOLOR,
				ColorLib.rgb(0, 0, 0));
		// use light grey for edges
		ColorAction edges = new ColorAction("tree.edges",
				VisualItem.STROKECOLOR, ColorLib.gray(200));

		// create an action list containing all color assignments
		ActionList color = new ActionList();
		color.add(fill);
		color.add(text);
		color.add(edges);

		// create an action list with an animated layout
		ActionList layout = new ActionList(Activity.INFINITY);
		layout.add(new NodeLinkTreeLayout("tree", Constants.ORIENT_TOP_BOTTOM,
				10, 10, 10));
		layout.add(new RepaintAction());

		// add the actions to the visualization
		vis.putAction("color", color);
		vis.putAction("layout", layout);

		// -- 5. the display and interactive controls -------------------------

		disp = new Display(vis);

		disp.addControlListener(new PanControl());
		disp.addControlListener(new ZoomControl());
		disp.addControlListener(new WheelZoomControl());

		String[] fields = { "Num Docs:" };
		String[] data = { "numdocs" };
		ToolTipControl control = new ToolTipControl(fields, data);
		disp.addControlListener(control);
	}
	
	public void setTree(Graph tree){
		this.tree = tree;
	}
	
	public void addDisplayToPanel(JPanel panel){
		disp.setPreferredSize(panel.getPreferredSize());
		disp.setMaximumSize(panel.getMaximumSize());
		panel.add(disp);
	}
	
	public void startVisualization(Dataset[] data){

		vis.add("tree", tree);
		
		vis.run("color");
		vis.run("layout");
	}

}
